﻿using MyWPF.StaticData;
using MyWPF.Views.Others;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32; //写入注册表时要用到

namespace MyWPF.Views
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = StaticModels.vM_MainWindow;
        }
        

        public void AddNews(string content)
        {
            Task.Factory.StartNew(() => {
                Dispatcher.Invoke(new Action(() =>
                {
                    News news = new News(content);
                    NewsRoom.Children.Add(news);

                    //如果消息大于10，就清除最早的一条
                    if (NewsRoom.Children.Count > 10)
                    {
                        NewsRoom.Children.RemoveAt(0);
                    }


                    StaticModels.vM_MainWindow.NewsNum = NewsRoom.Children.Count;
                }));
            }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);

            
        }

        private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
        {
            var parent = VisualTreeHelper.GetParent(dependencyObject);

            if (parent == null) return null;

            var parentT = parent as T;
            return parentT ?? FindParent<T>(parent);
        }

    }

}
